<%@ page language="java" contentType="text/html; charset=UTF8" pageEncoding="UTF8"%>
<%@ page import="java.util.*" %>
<%@ page import="bcnet.service.PriceHistoryService" %>
<%@ page import="bcnet.service.factory.PriceHistoryServiceFactory" %>
<%@ page import="bcnet.service.StockManager" %>
<%@ page import="bcnet.service.factory.StockManagerFactory" %>
<%@ page import="bcnet.domain.CodeVO" %>
<%@ page import="bcnet.domain.PriceHistoryVO" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.google.appengine.api.utils.SystemProperty" %>
<%@ page import="com.google.appengine.api.memcache.MemcacheService" %>
<%@ page import="com.google.appengine.api.memcache.MemcacheServiceFactory" %>
<%@ page import="com.google.appengine.api.memcache.ErrorHandlers" %>
<%@ page import="java.util.logging.Level" %>
<%

	String code = request.getParameter("code")==null?"1":request.getParameter("code");
	PriceHistoryService priceHistoryService = PriceHistoryServiceFactory.getPriceHistoryService();
	StockManager stockManager = StockManagerFactory.getStockManager();

	String line = stockManager.getResults(code,"1","stop",code);
	//System.out.println(line);
	
	String name = "";
	String suggestion = "";
	String np = "";
	String volume = "";
	//System.out.println(line);
	
	try{
		name = line.substring(line.indexOf("additionTxt\">")+13,line.indexOf("<br/>Suggestion")).trim();
		suggestion = line.substring(line.indexOf("<br/>Suggestion: ")+17,line.indexOf("<br/>Volume Stat")).trim();
		np = line.substring(line.indexOf(" np\">")+5,line.indexOf("&nbsp;",line.indexOf(" np\">"))).trim();
		volume = line.substring(line.indexOf("Volume: ")+8,line.indexOf(" (Pro:")).trim();
	}
	catch(Exception ex){ // no history
		name = "";
		suggestion = "";
		np = "";
		volume = "";
		//ex.printStackTrace();
	}
	//System.out.println("Name:"+name);
	//System.out.println("Suggestion:"+suggestion);
	//System.out.println(volume);
   	
	MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   	syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));

	Calendar cal = Calendar.getInstance();
	cal.add(Calendar.HOUR, +8);
	Date todayDate = cal.getTime();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String refreshStartTime = "<span>Refresh Start Time: "+sdf.format(todayDate)+"</span><br/>";
	
	if(syncCache.get("buyList") == null){
		syncCache.put("buyList",refreshStartTime);
	}   	
	
	if(syncCache.get("sellList") == null){
		syncCache.put("sellList",refreshStartTime);
	}   
	
   	
   	if(suggestion.equals("B")){
   		//priceHistoryService.addBuyList(code);	
  	 	priceHistoryService.addBuyList(code,name,np,volume);
   		//priceHistoryService.addBuyList(code,name,np);
   	}
  	 if(suggestion.equals("S")){
   		//priceHistoryService.addBuyList(code);	
  	 	priceHistoryService.addSellList(code,name,np,volume);
  		//priceHistoryService.addSellList(code,name,np);
   	}
  	 
  

 %>

